#include <retro.h>
#include "retro.library.h"
#include "retro.luainterface.h"

using namespace Retro;
using namespace Retro::Scripting;
using namespace SPLib::Geom;

ScriptManager::ScriptManager()
{
  mCallbacks.setSize(100);
  mInterfaces.setSize(5);
  mInterfaces.add(new LuaInterface());
}

ScriptManager::~ScriptManager()
{
  for (UINT i = 0; i < mInterfaces.getLength(); ++i)
  {
    delete mInterfaces[i];
  }
}

VOID    ScriptManager::add(ScriptValue* iValue)
{
  if (iValue->type == TypeFunction)
    mCallbacks.add((ScriptCallback)iValue->value);

  for (UINT i = 0; i < mInterfaces.getLength(); ++i)
    mInterfaces[i]->add(iValue);
}

VOID  ScriptManager::add(
  ScriptValue* iValues,
  UINT iValueCount,
  PCSTR iName
  )
{
  for (UINT i = 0; i < iValueCount; ++i)
  {
    if (iValues[i].type == TypeFunction)
      mCallbacks.add((ScriptCallback)iValues[i].value);
  }

  for (UINT i = 0; i < mInterfaces.getLength(); ++i)
    mInterfaces[i]->add(iValues, iValueCount, iName);
}

ScriptInterface*  ScriptManager::getInterface(Data::ScriptLanguage iLang)
{
  for (UINT i = 0; i < mInterfaces.getLength(); ++i)
  {
    if (mInterfaces[i]->getLanguage() == iLang)
      return mInterfaces[i];
  }

  return NULL;
}
